package org.example.algorithm.array;

public class NumArrayPrefixSolution {
    private int[] prefixSum;

    public NumArrayPrefixSolution(int[] nums) {
        prefixSum = new int[nums.length + 1];
        prefixSum[0] = 0;
        for (int i=1;i<=nums.length;i++) {
            prefixSum[i] = prefixSum[i-1] + nums[i-1];
        }
    }

    public int sumRange(int left, int right) {
        if (left == 0) {
            return prefixSum[right];
        } else {
            return prefixSum[right] - prefixSum[left-1];
        }
    }
}
